package com.lenovo.smarttraffic.ui.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.GsonBean.MetroInfoBean;
import com.lenovo.smarttraffic.ui.adapter.MyMetroRecyclerAdapter;
import com.lenovo.smarttraffic.ui.fragment.MetroPictureDialogFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Caleb.F
 * on 2019-11-25
 * at 8:56
 */
public class MetroLineActivity extends BaseActivity {

    private RecyclerView mMetroRecycler1;
    private RecyclerView mMetroRecycler2;
    private TextView mTvStartSpot;
    private TextView mTvStartSpotFirstTime;
    private TextView mTvStartSpotEndTime;
    private TextView mTvEndSpot;
    private TextView mTvEndspotFirstTime;
    private TextView mTvEndspotLastTime;
    private ImageView mImageMetroSrc;
    ;
    private TextView mTvMile;
    private TextView mTvTicket;
    private List<MetroInfoBean.ROWSDETAILBean> rows_detail;
    private FragmentManager fm;
    private String httpUrl;

    private static final String PORT = "8088";
    private static final String METRO_PICTURE_FRAGMENT = "com.lenovo.smarttraffic.ui.fragment.MetroPictureDialogFragment";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();
    }

    private void initView() {
        mMetroRecycler1 = findViewById(R.id.metro_recycler_1);
        mMetroRecycler2 = findViewById(R.id.metro_recycler_2);
        mTvStartSpot = findViewById(R.id.tv_start_spot);
        mTvStartSpotFirstTime = findViewById(R.id.tv_start_spot_first_time);
        mTvStartSpotEndTime = findViewById(R.id.tv_start_spot_end_time);
        mTvEndSpot = findViewById(R.id.tv_end_spot);
        mTvEndspotFirstTime = findViewById(R.id.tv_endspot_first_time);
        mTvEndspotLastTime = findViewById(R.id.tv_endspot_last_time);
        mImageMetroSrc = findViewById(R.id.image_metro_src);
        mTvMile = findViewById(R.id.tv_mile);
        mTvTicket = findViewById(R.id.tv_money);

    }


    Handler handler = new Handler(msg -> {
        switch (msg.what) {
            case 1:
                showData();
                initRecyclerView();
                break;
        }
        return false;
    });

    private void initRecyclerView() {
        mMetroRecycler1.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, false));
        mMetroRecycler2.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, false));
        ArrayList<String> string_recycler1 = new ArrayList<>();
        ArrayList<String> string_recycler2 = new ArrayList<>();
        for (int i = 0; i < rows_detail.get(0).getSites().size(); i++) {
            if (i % 2 == 0) {
                string_recycler1.add(rows_detail.get(0).getSites().get(i));
            } else {
                string_recycler2.add(rows_detail.get(0).getSites().get(i));

            }
        }
        MyMetroRecyclerAdapter recyclerAdapter1 = new MyMetroRecyclerAdapter(this, string_recycler1);
        MyMetroRecyclerAdapter recyclerAdapter2 = new MyMetroRecyclerAdapter(this, string_recycler2);
        mMetroRecycler1.setAdapter(recyclerAdapter1);
        mMetroRecycler2.setAdapter(recyclerAdapter2);
    }

    private void showData() {
        initToolBar("地铁" + rows_detail.get(0).getName() + "线路详情");
        mTvStartSpot.setText(rows_detail.get(0).getTime().get(0).getSite());
        mTvStartSpotFirstTime.setText(new StringBuilder().append("首班: ").append(rows_detail.get(0).getTime().get(0).getStarttime(), 0, 5));
        mTvStartSpotEndTime.setText(new StringBuilder().append("末班: ").append(rows_detail.get(0).getTime().get(0).getEndtime(), 0, 5));

        mTvEndSpot.setText(rows_detail.get(0).getTime().get(1).getSite());
        mTvEndspotFirstTime.setText(new StringBuilder().append("首班: ").append(rows_detail.get(0).getTime().get(1).getStarttime(), 0, 5));
        mTvEndspotLastTime.setText(new StringBuilder().append("末班: ").append(rows_detail.get(0).getTime().get(1).getEndtime(), 0, 5));
        int size = rows_detail.get(0).getSites().size();
        mTvMile.setText(new StringBuilder().append(size).append("站/").append((size - 1) * 2).append("公里"));
        mTvTicket.setText(new StringBuilder().append("票价: 最高票价").append(Math.round((size - 1) * 0.4)).append("元"));
        httpUrl = new StringBuilder().
                append("http://").append(NetWorkActivity.getIP(this)).append(":").append(PORT)
                .append("/transportservice").append(rows_detail.get(0).getMap()).toString();
        Glide.with(this).asBitmap().load(httpUrl).into(mImageMetroSrc);
        mImageMetroSrc.setOnClickListener(v -> {
            MetroPictureDialogFragment metroPictureDialogFragment = new MetroPictureDialogFragment();
            Bundle bundle = new Bundle();
            bundle.putString(Constant.METRO_PICTURE_URL, httpUrl);
            metroPictureDialogFragment.setArguments(bundle);
            fm = getSupportFragmentManager();
           fm.beginTransaction().add(metroPictureDialogFragment, METRO_PICTURE_FRAGMENT).commit();
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getMetroLine(MetroInfoBean metroInfoBean) {
        rows_detail = metroInfoBean.getROWS_DETAIL();
        Message message = new Message();
        message.what = 1;
        handler.sendMessage(message);
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_metro_line;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
